package com.alipay.app.groupmeal.standard.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;

public class ActionButton extends LinearLayout {

    TextView tv_an;
    TextView tv_button;
    TextView tv_action;
    TextView tv_cancel;
    ImageView iv_button;

    String btnName;
    String cancelBtnName;
    String actionName;
    Drawable btnDrawable;
    int textColor;
    boolean showIvButton;
    Drawable ivBtnDrawable;

    public ActionButton(Context context) {
        super(context);
    }

    public ActionButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BActionView);
        btnName = array.getString(R.styleable.BActionView_btnName);
        cancelBtnName = array.getString(R.styleable.BActionView_cancelBtnName);
        actionName = array.getString(R.styleable.BActionView_actionName);
        btnDrawable = array.getDrawable(R.styleable.BActionView_btnBackground);
        ivBtnDrawable = array.getDrawable(R.styleable.BActionView_ivBtnDrawable);
        textColor = array.getColor(R.styleable.BActionView_textColor, getContext().getColor(R.color.txt_666666));
        showIvButton = array.getBoolean(R.styleable.BActionView_showIvButton, false);
        array.recycle();

        LayoutInflater.from(context).inflate(R.layout.view_action_button, this, true);
        tv_an = findViewById(R.id.tv_an);
        tv_button = findViewById(R.id.tv_button);
        tv_action = findViewById(R.id.tv_action);
        tv_cancel = findViewById(R.id.tv_cancel);
        iv_button = findViewById(R.id.iv_button);
        if (!TextUtils.isEmpty(btnName)) {
            tv_button.setText(btnName);
        }
        if (btnDrawable != null) {
            tv_button.setBackground(btnDrawable);
        }
        if (!TextUtils.isEmpty(actionName)) {
            tv_action.setText(actionName);
        }
        if (!TextUtils.isEmpty(cancelBtnName) && tv_cancel != null) {
            tv_cancel.setText(cancelBtnName);
            tv_cancel.setVisibility(VISIBLE);
        }

        tv_an.setTextColor(textColor);
        tv_action.setTextColor(textColor);

        if (showIvButton) {
            iv_button.setVisibility(VISIBLE);
            tv_button.setVisibility(GONE);
            if (ivBtnDrawable != null) {
                iv_button.setBackground(ivBtnDrawable);
            }
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setFirstButtonName(String name) {
        if (tv_button != null) {
            tv_button.setText(name);
        }
    }

    public void setActionName(String name) {
        if (tv_action != null) {
            tv_action.setText(name);
        }
    }

    public void setSecondBtnName(String cancelBtnName) {
        tv_cancel.setText(cancelBtnName);
        tv_cancel.setVisibility(VISIBLE);
    }


    public void setFirstButtonTextAndColor(@ColorInt int color, int spSize) {
        if (tv_button != null) {
            tv_button.setTextColor(color);
            tv_button.setTextSize(TypedValue.COMPLEX_UNIT_SP, spSize);
        }
    }

    public void setActionTextAndColor(@ColorInt int color, int spSize) {
        if (tv_an != null) {
            tv_an.setTextColor(color);
            tv_an.setTextSize(TypedValue.COMPLEX_UNIT_SP, spSize);
        }
        if (tv_action != null) {
            tv_action.setTextColor(color);
            tv_action.setTextSize(TypedValue.COMPLEX_UNIT_SP, spSize);
        }
    }

}
